package com.sky.service;


import com.sky.entity.Dish;
import com.sky.entity.Orders;
import com.sky.mapper.DishMapper;
import com.sky.mapper.OrderMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;

import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.time.LocalDateTime;

@Component
@Slf4j
public class RabbitMQListener {

    @Autowired
    OrderMapper orderMapper;

    @RabbitListener(bindings = {@QueueBinding(
            value = @Queue(name = "dlx_boot_queue"),
            exchange = @Exchange("dlx_boot_routing_exchange"),
            key = "dlx_order")})
    public void letterQueue1(Message message) throws IOException {
//        System.out.println("aaaa");
        String idstr = new String(message.getBody());
        log.info("订单id：{}",idstr);
        Long id = Long.valueOf(idstr);
        log.info(">>>>>>>>>>>>>对id={}的订单进行支付超时判断",id);
        Orders orders = orderMapper.getById(id);
        //如果订单的状态在1分钟之后仍是1.即待付款，则自动将订单的状态改为6，已取消
        if(orders.getStatus().equals(1)){
            orders.setStatus(Orders.CANCELLED);
            orders.setCancelReason("订单超时，自动取消");
            orders.setCancelTime(LocalDateTime.now());
            orderMapper.update(orders);

        }
    }


//    @RabbitListener(bindings = {@QueueBinding(value = @Queue(name = "boot_queue",
//    arguments = {@Argument(name = "x-dead-letter-exchange",value = "dlx_boot_routing_exchange"),
//    @Argument(name = "x-dead-letter-routing-key",value = "dlx_order"),@Argument(name = "x-message-ttl",value = "100000",type = "java.lang.Integer")}),
//    exchange = @Exchange("boot_routing_exchange"),key = "order" )})
//    public void letterQueue2(Message message) throws IOException {
//        log.info("普通队列什么也不做");
//    }



}
